#include "general.h"
#if (SENSOR_TYPE == Sensor_GC1303)
#include "pv_api.h"
#include "sensor_cdsp.h"
#include "sp1k_aaa_api.h"
#include "sp1k_util_api.h"
#define HAL_FRONT_REMOVE_UNCALL_FUNCTION

UINT8 sensorDriverGainClass = 0xff;
UINT8	RawR[256]=0;
UINT8	RawG[256]=0;
UINT8	RawB[256]=0;
UINT16 last_gain;
UINT32 last_shutter;

extern  UINT8
kHAL_SetWBOffsetSet(
	UINT8 wboffseten,
	UINT8 gosvden,
	UINT16 ROffset,
	UINT16 GrOffset,
	UINT16 BOffset,
	UINT16 GbOffset,
	UINT8 wbovalidon
) ;
#define MAX_FRAME_RATE_30_IDX 111
#define MIN_FRAME_RATE_30_IDX 23
#define MAX_FRAME_RATE_15_IDX 111
#define MIN_FRAME_RATE_15_IDX 23

#define MAX_FRAME_RATE_25_IDX 111
#define MIN_FRAME_RATE_25_IDX 23
#define MAX_FRAME_RATE_12P5_IDX 111
#define MIN_FRAME_RATE_12P5_IDX 23

#define SENSOR_POWER	 14
//UINT16 ctc_rgain[4]={282, 348, 392, 0};
//UINT16 ctc_bgain[4]={321, 268, 211, 0};
UINT16 ctc_rgain[4]={207, 276, 357, 0};
UINT16 ctc_bgain[4]={439, 395, 296, 0};

#if 1
code AEstr LVtbl60[121] =
	{	{	50	,	300,		127	,	0	}	,
		{	51	,	300,		127	,	0	}	,
		{	52	,	300,		127	,	0	}	,
		{	53	,	300	,	124	,	0	}	,
		{	54	,	300	,	116	,	0	}	,
		{	55	,	300	,	106	,	0	}	,
		{	56	,	300	,	99	,	0	}	,
		{	57	,	300	,	91	,	0	}	,
		{	58	,	300	,	84	,	0	}	,
		{	59	,	300	,	76	,	0	}	,
		{	60	,	300	,	71	,	0	}	,
		{	61	,	300	,	67	,	0	}	,
		{	62	,	300	,	61	,	0	}	,
		{	63	,	300	,	57	,	0	}	,
		{	64	,	300	,	54	,	0	}	,
		{	65	,	300	,	50	,	0	}	,
		{	66	,	300	,	45	,	0	}	,
		{	67	,	300	,	42	,	0	}	,
		{	68	,	300	,	40	,	0	}	,
		{	69	,	300	,	37	,	0	}	,
		{	70	,	300	,	34	,	0	}	,
		{	71	,	300	,	30	,	0	}	,
		{	72	,	300	,	28	,	0	}	,
		{	73	,	300	,	26	,	0	}	,
		{	74	,	300	,	25	,	0	}	,
		{	75	,	300	,	24	,	0	}	,
		{	76	,	300	,	21	,	0	}	,
		{	77	,	300	,	20	,	0	}	,
		{	78	,	300	,	19	,	0	}	,
		{	79	,	300	,	18	,	0	}	,
		{	80	,	300	,	17	,	0	}	,
		{	81	,	300	,	15	,	0	}	,
		{	82	,	400	,	29	,	0	}	,
		{	83	,	400	,	27	,	0	}	,
		{	84	,	400	,	24	,	0	}	,
		{	85	,	400	,	22	,	0	}	,
		{	86	,	600	,	21	,	0	}	,
		{	87	,	600	,	20	,	0	}	,
		{	88	,	600	,	19	,	0	}	,
		{	89	,	600	,	18	,	0	}	,
		{	90	,	600	,	16	,	0	}	,
		{	91	,	600	,	15	,	0	}	,
		{	92	,	1200	,	29	,	0	}	,
		{	93	,	1200	,	27	,	0	}	,
		{	94	,	1200	,	24	,	0	}	,
		{	95	,	1200	,	22	,	0	}	,
		{	96	,	1200	,	21	,	0	}	,
		{	97	,	1200	,	20	,	0	}	,
		{	98	,	1200	,	18	,	0	}	,
		{	99	,	1200	,	17	,	0	}	,
		{	100	,	1200	,	16	,	0	}	,
		{	101	,	1200	,	13	,	0	}	,
		{	102	,	1252	,	15	,	0	}	,
		{	103	,	1342	,	15	,	0	}	,
		{	104	,	1439	,	15	,	0	}	,
		{	105	,	1543	,	15	,	0	}	,
		{	106	,	1654	,	15	,	0	}	,
		{	107	,	1773	,	15	,	0	}	,
		{	108	,	1901	,	15	,	0	}	,
		{	109	,	2038	,	15	,	0	}	,
		{	110	,	2185	,	15	,	0	}	,
		{	111	,	2342	,	15	,	0	}	,
		{	112	,	2511	,	15	,	0	}	,
		{	113	,	2692	,	15	,	0	}	,
		{	114	,	2886	,	15	,	0	}	,
		{	115	,	3094	,	15	,	0	}	,
		{	116	,	3317	,	15	,	0	}	,
		{	117	,	3556	,	15	,	0	}	,
		{	118	,	3812	,	15	,	0	}	,
		{	119	,	4086	,	15	,	0	}	,
		{	120	,	4380	,	15	,	0	}	,
		{	121	,	4695	,	15	,	0	}	,
		{	122	,	5032	,	15	,	0	}	,
		{	123	,	5394	,	15	,	0	}	,
		{	124	,	5782	,	15	,	0	}	,
		{	125	,	6198	,	15	,	0	}	,
		{	126	,	6643	,	16	,	0	}	,
		{	127	,	7120	,	16	,	0	}	,
		{	128	,	7632	,	16	,	0	}	,
		{	129	,	8180	,	16	,	0	}	,
		{	130	,	8768	,	16	,	0	}	,
		{	131	,	9398	,	16	,	0	}	,
		{	132	,	10073	,	16	,	0	}	,
		{	133	,	10796	,	16	,	0	}	,
		{	134	,	11000	,	15	,	0	}	,
		{	135	,	12402	,	16	,	0	}	,
		{	136	,	13000	,	16	,	0	}	,
		{	137	,	15000	,	17	,	0	}	,
		{	138	,	15000	,	16	,	0	}	,
		{	139	,	17000	,	16	,	0	}	,
		{	140	,	17000	,	15	,	0	}	,
		{	141	,	19000	,	17	,	0	}	,
		{	142	,	19000	,	16	,	0	}	,
		{	143	,	22000	,	16	,	0	}	,
		{	144	,	22000	,	15	,	0	}	,
		{	145	,	25000	,	18	,	0	}	,
		{	146	,	25000	,	17	,	0	}	,
		{	147	,	25000	,	16	,	0	}	,
		{	148	,	30549	,	17	,	0	}	,
		{	149	,	30549	,	16	,	0	}	,
		{	150	,	30549 ,	 	15,	 0	}	,
		{	151	,	40000,	 	22,	 0	}	,
		{	152	,	40000,	 	20,	 0	}	,
		{	153	,	40000,	 	19,	 0	}	,
		{	154	,	40000,	 	18,	 0	}	,
		{	155	,	40000,	 	17,	 0	}	,
		{	156	,	40000,	 	16,	 0	}	,
		{	157	,	40000,	 	15,	 0	}	,
		{	158	,	65000,	 	17,	 0	}	,
		{	159	,	65000,	 	16,	 0	}	,
		{	160	,	65000,	 	15,	 0	}	,
		{	161	,	65000,	 	14,	 0	}	,
		{	162	,	137997,	 	46,	 0	}	,
		{	163	,	137997, 		43,	 0	}	,
		{	164	,	137997,	 	40,	 0	}	,
		{	165	,	137997, 		36,	 0	}	,
		{	166	,	137997, 	34,	 0	}	,
		{	167	,	137997,		32,	 0	}	,
		{	168	,	137997, 	30,	 0	}	,
		{	169	,	137997, 	28,	 0	}	,
		{	170	,	137997, 	26,	 0	}
	};

code AEstr LVtbl50[121] =
	{	{	50	,	333	,	127	,	0	}	,
		{	51	,	333	,	127	,	0	}	,
		{	52	,	333	,	127	,	0	}	,
		{	53	,	333	,	127	,	0	}	,
		{	54	,	333	,	127	,	0	}	,
		{	55	,	333	,	120	,	0	}	,
		{	56	,	333	,	110	,	0	}	,
		{	57	,	333	,	103	,	0	}	,
		{	58	,	333	,	97	,	0	}	,
		{	59	,	333	,	88	,	0	}	,
		{	60	,	333	,	83	,	0	}	,
		{	61	,	333	,	75	,	0	}	,
		{	62	,	333	,	70	,	0	}	,
		{	63	,	333	,	66	,	0	}	,
		{	64	,	333	,	60	,	0	}	,
		{	65	,	333	,	56	,	0	}	,
		{	66	,	333	,	52	,	0	}	,
		{	67	,	333	,	47	,	0	}	,
		{	68	,	333	,	44	,	0	}	,
		{	69	,	333	,	41	,	0	}	,
		{	70	,	333	,	39	,	0	}	,
		{	71	,	333	,	36	,	0	}	,
		{	72	,	333	,	33	,	0	}	,
		{	73	,	333	,	30	,	0	}	,
		{	74	,	333	,	28	,	0	}	,
		{	75	,	333	,	26	,	0	}	,
		{	76	,	333	,	25	,	0	}	,
		{	77	,	333	,	23	,	0	}	,
		{	78	,	333	,	21	,	0	}	,
		{	79	,	333	,	20	,	0	}	,
		{	80	,	333	,	18	,	0	}	,
		{	81	,	333	,	17	,	0	}	,
		{	82	,	333	,	16	,	0	}	,
		{	83	,	500	,	23	,	0	}	,
		{	84	,	500	,	21	,	0	}	,
		{	85	,	500	,	19	,	0	}	,
		{	86	,	500	,	18	,	0	}	,
		{	87	,	500	,	17	,	0	}	,
		{	88	,	500	,	16	,	0	}	,
		{	89	,	1000	,	30	,	0	}	,
		{	90	,	1000	,	27	,	0	}	,
		{	91	,	1000	,	26	,	0	}	,
		{	92	,	1000	,	24	,	0	}	,
		{	93	,	1000	,	23	,	0	}	,
		{	94	,	1000	,	21	,	0	}	,
		{	95	,	1000	,	20	,	0	}	,
		{	96	,	1000	,	18	,	0	}	,
		{	97	,	1000	,	17	,	0	}	,
		{	98	,	1000	,	16	,	0	}	,
		{	99	,	1016	,	15	,	0	}	,
		{	100	,	1089	,	15	,	0	}	,
		{	101	,	1168	,	15	,	0	}	,
		{	102	,	1252	,	15	,	0	}	,
		{	103	,	1342	,	15	,	0	}	,
		{	104	,	1439	,	15	,	0	}	,
		{	105	,	1543	,	15	,	0	}	,
		{	106	,	1654	,	15	,	0	}	,
		{	107	,	1773	,	15	,	0	}	,
		{	108	,	1901	,	15	,	0	}	,
		{	109	,	2038	,	15	,	0	}	,
		{	110	,	2185	,	15	,	0	}	,
		{	111	,	2342	,	15	,	0	}	,
		{	112	,	2511	,	15	,	0	}	,
		{	113	,	2692	,	15	,	0	}	,
		{	114	,	2886	,	15	,	0	}	,
		{	115	,	3094	,	15	,	0	}	,
		{	116	,	3317	,	15	,	0	}	,
		{	117	,	3556	,	15	,	0	}	,
		{	118	,	3812	,	15	,	0	}	,
		{	119	,	4086	,	15	,	0	}	,
		{	120	,	4380	,	15	,	0	}	,
		{	121	,	4695	,	15	,	0	}	,
		{	122	,	5032	,	15	,	0	}	,
		{	123	,	5394	,	15	,	0	}	,
		{	124	,	5782	,	15	,	0	}	,
		{	125	,	6198	,	15	,	0	}	,
		{	126	,	6643	,	16	,	0	}	,
		{	127	,	7120	,	16	,	0	}	,
		{	128	,	7632	,	16	,	0	}	,
		{	129	,	8180	,	16	,	0	}	,
		{	130	,	8768	,	16	,	0	}	,
		{	131	,	9398	,	16	,	0	}	,
		{	132	,	10073	,	16	,	0	}	,
		{	133	,	10796	,	16	,	0	}	,
		{	134	,	11000	,	15	,	0	}	,
		{	135	,	12402	,	16	,	0	}	,
		{	136	,	13000	,	16	,	0	}	,
		{	137	,	15000	,	17	,	0	}	,
		{	138	,	15000	,	16	,	0	}	,
		{	139	,	17000	,	16	,	0	}	,
		{	140	,	17000	,	15	,	0	}	,
		{	141	,	19000	,	17	,	0	}	,
		{	142	,	19000	,	16	,	0	}	,
		{	143	,	22000	,	16	,	0	}	,
		{	144	,	22000	,	15	,	0	}	,
		{	145	,	25000	,	18	,	0	}	,
		{	146	,	25000	,	17	,	0	}	,
		{	147	,	25000	,	16	,	0	}	,
		{	148	,	30549	,	17	,	0	}	,
		{	149	,	30549	,	16	,	0	}	,
		{	150	,	30549 ,	 	15,	 0	}	,
		{	151	,	40000,	 	22,	 0	}	,
		{	152	,	40000,	 	20,	 0	}	,
		{	153	,	40000,	 	19,	 0	}	,
		{	154	,	40000,	 	18,	 0	}	,
		{	155	,	40000,	 	17,	 0	}	,
		{	156	,	40000,	 	16,	 0	}	,
		{	157	,	40000,	 	15,	 0	}	,
		{	158	,	65000,	 	17,	 0	}	,
		{	159	,	65000,	 	16,	 0	}	,
		{	160	,	65000,	 	15,	 0	}	,
		{	161	,	65000,	 	14,	 0	}	,
		{	162	,	137997,	 	46,	 0	}	,
		{	163	,	137997, 		43,	 0	}	,
		{	164	,	137997,	 	40,	 0	}	,
		{	165	,	137997, 		36,	 0	}	,
		{	166	,	137997, 	34,	 0	}	,
		{	167	,	137997,		32,	 0	}	,
		{	168	,	137997, 	30,	 0	}	,
		{	169	,	137997, 	28,	 0	}	,
		{	170	,	137997, 	26,	 0	}
	};
#endif

#define MAX_GAIN_IDX (32)
#define MIN_GAIN_IDX 5

code UINT16 gaintbl[128] = {
0x1000,0x1010,0x1020,0x1030,0x1040,0x1050,0x1060,0x1070,0x1080,0x1090,0x10a0,0x10b0,0x10c0,0x10d0,0x10e0,0x10f0,
0x1100,0x1110,0x1120,0x1130,0x1140,0x1150,0x1160,0x1170,0x1180,0x1190,0x11a0,0x11b0,0x11c0,0x11d0,0x11e0,0x11f0,
0x2200,0x2210,0x2220,0x2230,0x2240,0x2250,0x2260,0x2270,0x2280,0x2290,0x22a0,0x22b0,0x22c0,0x22d0,0x22e0,0x22f0,
0x3300,0x3310,0x3320,0x3330,0x3340,0x3350,0x3360,0x3370,0x3380,0x3390,0x33a0,0x33b0,0x33c0,0x33d0,0x33e0,0x33f0,
0x4400,0x4410,0x4420,0x4430,0x4440,0x4450,0x4460,0x4470,0x4480,0x4490,0x44a0,0x44b0,0x44c0,0x44d0,0x44e0,0x44f0,
0x5500,0x5510,0x5520,0x5530,0x5540,0x5550,0x5560,0x5570,0x5580,0x5590,0x55a0,0x55b0,0x55c0,0x55d0,0x55e0,0x55f0,
0x6600,0x6610,0x6620,0x6630,0x6640,0x6650,0x6660,0x6670,0x6680,0x6690,0x66a0,0x66b0,0x66c0,0x66d0,0x66e0,0x66f0,
0x7700,0x7710,0x7720,0x7730,0x7740,0x7750,0x7760,0x7770,0x7780,0x7790,0x77a0,0x77b0,0x77c0,0x77d0,0x77e0,0x77f0
};

#define K_GAIN_Large        90
#define K_GAIN_Middle		50
#define K_GAIN_Small		30

#define MCLKMODE        0 /*mclk mode,'1'from I/O Pad,'0'from internal*/
#define PCLKMODE        1 /*pclk mode,'1'from I/O Pad,'0'from internal*/
#define PREV_MCLKDIV    4 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define PREV_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#define SNAP_MCLKDIV    4 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define SNAP_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/


code UINT16 cmt[3][9] = {
	{0x006C,0x0391,0x0043,0x000B,0x03F2,0x0043,0x0014,0x0377,0x00B5},
	{0x005B,0x03A4,0x0041,0x000F,0x03EE,0x0043,0x0015,0x038B,0x009F},
	{0x0046,0x03B9,0x0041,0x0015,0x03E7,0x0044,0x0019,0x03A6,0x0081}};

#if (PCLKMODE == 1)
/* FREQUENCY unity is KHz */
#define PREV_PCLKFREQ (UINT32) 24000000
#define SNAP_PCLKFREQ (UINT32) 24000000
#define CLK_RATIO     (UINT8) ((SNAP_PCLKFREQ/1000) * 128 / (PREV_PCLKFREQ/1000))
#else
#define PREV_PCLKFREQ (UINT32) 96000000 / PREV_MCLKDIV / PREV_PCLKDIV
#define SNAP_PCLKFREQ (UINT32) 96000000 / SNAP_MCLKDIV / SNAP_PCLKDIV
#define CLK_RATIO     (UINT8) ((UINT16) PREV_MCLKDIV * PREV_PCLKDIV * 128 / SNAP_MCLKDIV / SNAP_PCLKDIV)
#endif

#define PCLKphase 0
#define TGGPIOFUNC 0
#define TGGPIODIR 0x04
#define TGGPIOVAL 0

xdata UINT16 exptime;

extern xdata UINT32 G_SHUTTER;
extern xdata UINT8 G_GAIN;
extern xdata UINT8 PreGain;
extern xdata UINT32 PreShutter;
extern UINT8 HAL_CDSPeofWait( UINT8 Number);
extern void cdspColorMatrixRestore_Intr(void);
extern void cdspColorMatrixRestore(void);


extern xdata UINT32 G_SHUTTER;
extern xdata UINT8 G_GAIN;
extern xdata UINT8 G_ExpGainSetPos;
extern xdata UINT8 G_GainAfterExp;

extern UINT8 HAL_CDSPeofWait( UINT8 Number);
void  HAL_GCI2C_Write(UINT8 RegAddr, UINT8 RegData)
{
  UINT8 reg_addr[1], reg_data[1];
  reg_addr[0] = RegAddr;
  reg_data[0] = RegData;
  HAL_WriteSSC(reg_addr, reg_data, 0x01, 0x00);  //Set Seq SSC Write
}

void  HAL_FrontInit (UINT8 CamMode) USING_0
{
	UINT8 i;

	printf (" Initialize the Front Sensor_GC1303 : Enter !!,CamMode=%d \n",CamMode);
	sp1kGpioByteFuncSet(SP1K_GPIO_BYTE_GEN1,0xc0,0xc0);
	sp1kGpioByteDirSet(SP1K_GPIO_BYTE_GEN1,0x40,0x40);//gpio14
	sp1kGpioByteOutSet(SP1K_GPIO_BYTE_GEN1,0x40,0x40);//SP1K_GPIO_BYTE_GEN1
	/*for(i=0;i<100;i++)
	DELAY_1ms();
	XBYTE[0x2c02]|=0x03;
	//P1|=0x1;//power down
	P1&=0xfd;//reset
	for(i=0;i<100;i++)
	DELAY_1ms();
	P1|=0x2;*/

	HAL_SetFrontIOConfig(0x00, 0x00, 0x04, 0x00);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	HAL_SSCConfig(Sensor_Slave_Address, Sensor_SSC_Freq);
	XBYTE[0x2AB0] = 0x01;
	XBYTE[0x2AB0] = 0x00;
	G_ExpGainSetPos = 0x01;
	G_GainAfterExp = 0x00;
	XBYTE[0x2Ad0] = XBYTE[0x2Ad0] | 0x20;
	HAL_SensorRegInit();
}

void  HAL_FrontPreviewSet(void) USING_0
{
  HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
  HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
  HAL_SetFrontImgCrop(Sensor_Preview_HOffset, Sensor_Preview_VOffset, Sensor_Preview_HSize, Sensor_Preview_VSize, 0x00);
  HAL_SetFrontInverse(Prev_Hsync_Inverse,Prev_Vsync_Inverse);
  HAL_SetFrontReshape(Sensor_Preview_Hfall, Sensor_Preview_Hrise, Sensor_Preview_HReshen, Sensor_Preview_Vfall, Sensor_Preview_Vrise, Sensor_Preview_VReshen);
  HAL_SetSensorPreview();
  XBYTE[0x2110]=Sensor_Preview_BayerPattern;

}

void  HAL_FrontSnapSet(UINT16 Hsize,UINT16 Vsize) USING_0
{
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(SNAP_MCLKDIV, SNAP_PCLKDIV, 0);
	HAL_SetFrontReshape(Sensor_Snap_Hfall, Sensor_Snap_Hrise, Sensor_Snap_HReshen, Sensor_Snap_Vfall, Sensor_Snap_Vrise, Sensor_Snap_VReshen);
	HAL_SetFrontImgCrop(Sensor_Snap_HOffset, Sensor_Snap_VOffset, Hsize, Vsize, 1);
	XBYTE[0x2110]=Sensor_Snap_BayerPattern;
	HAL_GCI2C_Write(0x01,0x62);
	HAL_GCI2C_Write(0x02,0x19);
	HAL_GCI2C_Write(0x05,0x00);
	HAL_GCI2C_Write(0x06,0x00);
	HAL_GCI2C_Write(0x07,0x00);
	HAL_GCI2C_Write(0x08,0x00);
	HAL_GCI2C_Write(0x09,0x00);
	HAL_GCI2C_Write(0x12,0x00);
	HAL_GCI2C_Write(0x13,0x04);
	HAL_GCI2C_Write(0x14,0x00);
	HAL_GCI2C_Write(0x15,0x0a);
	HAL_GCI2C_Write(0x16,0x04);
	HAL_GCI2C_Write(0x17,0x08);
	HAL_GCI2C_Write(0x18,0x05);
	HAL_GCI2C_Write(0x19,0x08);
	HAL_GCI2C_Write(0x1b,0x01);
	HAL_GCI2C_Write(0x1c,0x10);
	HAL_GCI2C_Write(0x1d,0x29);
	HAL_GCI2C_Write(0x1f,0x08);
	HAL_GCI2C_Write(0x20,0x00);
	HAL_GCI2C_Write(0x21,0x00);
	HAL_GCI2C_Write(0x22,0x4a);//0x48
	HAL_GCI2C_Write(0x23,0x00);
	HAL_GCI2C_Write(0x24,0x20);

	PreGain =0;
	PreShutter = 0;
}

void  HAL_SensorRegInit(void)
{
	HAL_GCI2C_Write(0x25,0x00);
	HAL_Wait(100);
	HAL_GCI2C_Write(0x25,0x01);
	HAL_GCI2C_Write(0x00,0x11);
	HAL_GCI2C_Write(0x01,0x7b);
	HAL_GCI2C_Write(0x02,0x01);
	HAL_GCI2C_Write(0x03,0x03);
	HAL_GCI2C_Write(0x04,0x0d);
	HAL_GCI2C_Write(0x05,0x00);
	HAL_GCI2C_Write(0x06,0x00);
	HAL_GCI2C_Write(0x07,0x00);
	HAL_GCI2C_Write(0x08,0x00);
	HAL_GCI2C_Write(0x09,0x00);
	HAL_GCI2C_Write(0x0a,0x00);
	HAL_GCI2C_Write(0x0b,0xa0);
	HAL_GCI2C_Write(0x0c,0x00);
	HAL_GCI2C_Write(0x0d,0xa0);
	HAL_GCI2C_Write(0x0e,0x00);
	HAL_GCI2C_Write(0x0f,0xa0);
	HAL_GCI2C_Write(0x10,0x00);
	HAL_GCI2C_Write(0x11,0xa0);
	HAL_GCI2C_Write(0x12,0x00);
	HAL_GCI2C_Write(0x13,0x04);
	HAL_GCI2C_Write(0x14,0x00);
	HAL_GCI2C_Write(0x15,0x0a);
	HAL_GCI2C_Write(0x16,0x04);
	HAL_GCI2C_Write(0x17,0x08);
	HAL_GCI2C_Write(0x18,0x05);
	HAL_GCI2C_Write(0x19,0x08);
	//HAL_GCI2C_Write(0x1a,0x00);
	HAL_GCI2C_Write(0x1b,0x01);
	HAL_GCI2C_Write(0x1c,0x12);
	HAL_GCI2C_Write(0x1d,0x2a);

	HAL_GCI2C_Write(0x1f,0x08);
	HAL_GCI2C_Write(0x20,0x24);
	HAL_GCI2C_Write(0x21,0x05);
	HAL_GCI2C_Write(0x22,0x8a);
	HAL_GCI2C_Write(0x23,0x00);
	HAL_GCI2C_Write(0x24,0x20);
	HAL_GCI2C_Write(0x25,0x01);
	HAL_GCI2C_Write(0x1e,0x10);
	HAL_Wait(100);
	HAL_GCI2C_Write(0x1e,0x00);
	HAL_GCI2C_Write(0x25,0x01);
}

void HAL_SetSensorPreview(void) USING_0
{
//  HAL_GCI2C_Write(0x00,0x11);//Chip Version
  HAL_GCI2C_Write(0x01,/*0x74*/0x7b);//Horizontal Blank Line Register  0x7b
  HAL_GCI2C_Write(0x02,/*0x19*/0x01);//Vertical Blank Line Register
 // HAL_GCI2C_Write(0x03,0x09);
//  HAL_GCI2C_Write(0x04,0xb4);
  HAL_GCI2C_Write(0x05,0x00);//Global Background Composition
  HAL_GCI2C_Write(0x06,0x00);//GRED Background Composition
  HAL_GCI2C_Write(0x07,0x00);//RED Background Composition
  HAL_GCI2C_Write(0x08,0x00);//BLUE Background Composition
  HAL_GCI2C_Write(0x09,0x00);//GBLUE Background Composition
 /* HAL_GCI2C_Write(0x0a,0x00);//GRED Gain Control High Register
  HAL_GCI2C_Write(0x0b,0x66);//GRED Gain Control Low Register
  HAL_GCI2C_Write(0x0c,0x00);//RED Gain Control High Register
  HAL_GCI2C_Write(0x0d,0x7f);//RED Gain Control Low Register
  HAL_GCI2C_Write(0x0e,0x00);//BLUE Gain Control High Register
  HAL_GCI2C_Write(0x0f,0x7f);//BLUE Gain Control Low Register
  HAL_GCI2C_Write(0x10,0x00);//GBLUE Gain Control High Register
  HAL_GCI2C_Write(0x11,0x7e);//GBLUE Gain Control Low Register*/
  HAL_GCI2C_Write(0x12,0x00);//Row Start High Control Register
  HAL_GCI2C_Write(0x13,0x04);//Row Start Low Control Register
  HAL_GCI2C_Write(0x14,0x00);//Col Start High Control Register
  HAL_GCI2C_Write(0x15,0x00);//Col Start Low Control Register++//00
  HAL_GCI2C_Write(0x16,0x04);//Window Height High Control Register  1032
  HAL_GCI2C_Write(0x17,0x08);//Window Height Low Control Register
  HAL_GCI2C_Write(0x18,0x05);//Window Width High Control Register   1288
  HAL_GCI2C_Write(0x19,0x10);//Window Width Low Control Register
  HAL_GCI2C_Write(0x1b,0x01);//Black Level Calibration Register
  HAL_GCI2C_Write(0x1c,0x10);//A/D Bias Control Register
  HAL_GCI2C_Write(0x1d,0x29);//Col Bias Current Control Register
  HAL_GCI2C_Write(0x1f,0x08);//Timing Control Register
  HAL_GCI2C_Write(0x20,0x24);//Sub Sampling Contro ------ preview to capture convert
  HAL_GCI2C_Write(0x21,0x05);//Sub Sampling Mode Control++  0x05
  HAL_GCI2C_Write(0x22,0x8a);//Frame Control Register++  0x88
 // HAL_GCI2C_Write(0x22,0x48);
  HAL_GCI2C_Write(0x23,0x00);//VSYNC Output Control
  HAL_GCI2C_Write(0x24,0x20);//Output Control
 /* */HAL_GCI2C_Write(0x1e,0x10);//Test Control
  HAL_Wait(100);
  HAL_GCI2C_Write(0x1e,0x00);//Test Control
  HAL_GCI2C_Write(0x25,0x00);//SoftReset Control
  HAL_Wait(100);
  HAL_GCI2C_Write(0x25,0x01);//SoftReset Control
 }
/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontGainSet                                                     *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Sensor gain                                                           *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust gain value for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, gain value to set                                                    *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT8 HAL_FrontGainSet(
    UINT32 value,
    UINT32  option
)
{
#if 1
	UINT32 tmp0;
	UINT8 regaddr[8], regdata[8];
	UINT16 gainvalue;
	UINT8 ob;
	tmp0 = option & 0x01;                 /* Sync VD ? */

	gainvalue=gaintbl[ value];
	//ob=(UINT8)(gainvalue>>12);
	last_gain = gainvalue;
	/*switch(ob)
	{
		case 1:
			kHAL_SetWBOffsetSet(1,0, 0x3f0, 0x3f0, 0x3f0,0x3f0,1);
			break;
		case 2:
			kHAL_SetWBOffsetSet(1,0, 0x3e0, 0x3e0, 0x3e0,0x3e0,1);
			break;
		case 3:
			kHAL_SetWBOffsetSet(1,0, 0x3d0, 0x3d0, 0x3d0,0x3d0,1);
			break;
		case 4:
			kHAL_SetWBOffsetSet(1,0, 0x3c0, 0x3c0, 0x3c0,0x3c0,1);
			break;
		case 5:
			kHAL_SetWBOffsetSet(1,0, 0x3b0, 0x3b0, 0x3b0,0x3b0,1);
			break;
		case 6:
			kHAL_SetWBOffsetSet(1,0, 0x3a0, 0x3a0, 0x3a0,0x3a0,1);
			break;
		case 7:
			kHAL_SetWBOffsetSet(1,0, 0x390, 0x390, 0x390,0x390,1);
			break;

	}*/

	regaddr[0] = 0x0a;
	regdata[0]=((gainvalue>>8)&0x0f);
	regaddr[1] = 0x0b;
	regdata[1]=(gainvalue&0xff);
	regaddr[2] = 0x0c;
	regdata[2]=((gainvalue>>8)&0x0f);
	regaddr[3] = 0x0d;
	regdata[3]=(gainvalue&0xff);
	regaddr[4] = 0x0e;
	regdata[4]=((gainvalue>>8)&0x0f);
	regaddr[5] = 0x0f;
	regdata[5]=(gainvalue&0xff);
	regaddr[6] = 0x10;
	regdata[6]=((gainvalue>>8)&0x0f);
	regaddr[7] = 0x11;
	regdata[7]=(gainvalue&0xff);
       HAL_WriteSSC(regaddr, regdata, 0x08, tmp0|0x02);

#endif
	return SUCCESS;
}

UINT8 HAL_FrontGainSet_Intr(
    UINT32 value,
    UINT32  option
)USING_1
{
#if 1
	UINT32 tmp0;
	UINT8 regaddr[8], regdata[8];
	UINT16 gainvalue;
	UINT8 ob;
	tmp0 = option & 0x01;                 /* Sync VD ? */

	gainvalue=gaintbl[ value];
	//ob=(UINT8)(gainvalue>>12);
	last_gain = gainvalue;
	/*switch(ob)
	{
		case 1:
			kHAL_SetWBOffsetSet(1,0, 0x3f0, 0x3f0, 0x3f0,0x3f0,1);
			break;
		case 2:
			kHAL_SetWBOffsetSet(1,0, 0x3e0, 0x3e0, 0x3e0,0x3e0,1);
			break;
		case 3:
			kHAL_SetWBOffsetSet(1,0, 0x3d0, 0x3d0, 0x3d0,0x3d0,1);
			break;
		case 4:
			kHAL_SetWBOffsetSet(1,0, 0x3c0, 0x3c0, 0x3c0,0x3c0,1);
			break;
		case 5:
			kHAL_SetWBOffsetSet(1,0, 0x3b0, 0x3b0, 0x3b0,0x3b0,1);
			break;
		case 6:
			kHAL_SetWBOffsetSet(1,0, 0x3a0, 0x3a0, 0x3a0,0x3a0,1);
			break;
		case 7:
			kHAL_SetWBOffsetSet(1,0, 0x390, 0x390, 0x390,0x390,1);
			break;

	}*/

	regaddr[0] = 0x0a;
	regdata[0]=((gainvalue>>8)&0x0f);
	regaddr[1] = 0x0b;
	regdata[1]=(gainvalue&0xff);
	regaddr[2] = 0x0c;
	regdata[2]=((gainvalue>>8)&0x0f);
	regaddr[3] = 0x0d;
	regdata[3]=(gainvalue&0xff);
	regaddr[4] = 0x0e;
	regdata[4]=((gainvalue>>8)&0x0f);
	regaddr[5] = 0x0f;
	regdata[5]=(gainvalue&0xff);
	regaddr[6] = 0x10;
	regdata[6]=((gainvalue>>8)&0x0f);
	regaddr[7] = 0x11;
	regdata[7]=(gainvalue&0xff);
       HAL_WriteSSC(regaddr, regdata, 0x08, tmp0|0x02);

#endif
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontExpTimeSet                                                  *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control exposure time                                                         *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust exposure time value                                                       *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, expossure time value to set, unit 0.1ms                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT32
HAL_FrontExpTimeSet(
	UINT32 value,
	UINT32 option
)
{
#if 1
	UINT16 ltotal, ftotal, espline;
	UINT32 pixclk;
	UINT8  regaddr[2], regdata[2];

	ltotal = Sensor_Preview_LineTotal;                        /* Read line total */
	ftotal = Sensor_Preview_FrameTotal;                     /* Read frame total */
	pixclk = PREV_PCLKFREQ;
	last_shutter=value;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;
	espline = espline<<1;
	regaddr[0] = 0x03;
	regdata[0] = ((espline>>8)& 0xFF);
	regaddr[1] = 0x04;
	regdata[1] =(espline& 0xFF);
	HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);

#endif
return SUCCESS;
}

UINT32
HAL_FrontExpTimeSet_Intr(
	UINT32 value,
	UINT32 option
)USING_1
{
#if 1
	UINT16 ltotal, ftotal, espline;
	UINT32 pixclk;
	UINT8  regaddr[2], regdata[2];

	ltotal = Sensor_Preview_LineTotal;                        /* Read line total */
	ftotal = Sensor_Preview_FrameTotal;                     /* Read frame total */
	pixclk = PREV_PCLKFREQ;
	last_shutter=value;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;
	espline = espline<<1;
	regaddr[0] = 0x03;
	regdata[0] = ((espline>>8)& 0xFF);
	regaddr[1] = 0x04;
	regdata[1] =(espline& 0xFF);
	HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);

#endif
return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontFrameRateSet                                                *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Frame Rate                                                            *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust Frame rate for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, frame rate value to set                                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT32
HAL_FrontFrameRateSet(
    UINT32 value,
    UINT32 option
)
{
	 return 0;

}

void HAL_ExpTransToSnap(void)
{

#if 1
	UINT16 ltotal, ftotal,espline;
	UINT32 tmp0,pixclk;
	UINT8  regaddr[10], regdata[10];
	UINT16  s_gain,lastt_gain;
	UINT8 ob;

	ltotal = Sensor_Snap_LineTotal;						/* Read line total */
	ftotal = Sensor_Snap_FrameTotal;					/* Read frame total */
	pixclk = SNAP_PCLKFREQ;

	espline = (pixclk * 10) / (ltotal * last_shutter);
	s_gain = last_gain ;

	lastt_gain=s_gain;

	cdspColorMatrixRestore();
	XBYTE[0x21D3] = 0x02;
	XBYTE[0x21D0] = 0x02;
	sensorDriverGainClass = 0;

	/*ob=(UINT8)(lastt_gain>>12);
	switch(ob)
	{
		case 1:
			kHAL_SetWBOffsetSet(1,0, 0x3f0, 0x3f0, 0x3f0,0x3f0,1);
			break;
		case 2:
			kHAL_SetWBOffsetSet(1,0, 0x3e0, 0x3e0, 0x3e0,0x3e0,1);
			break;
		case 3:
			kHAL_SetWBOffsetSet(1,0, 0x3d0, 0x3d0, 0x3d0,0x3d0,1);
			break;
		case 4:
			kHAL_SetWBOffsetSet(1,0, 0x3c0, 0x3c0, 0x3c0,0x3c0,1);
			break;
		case 5:
			kHAL_SetWBOffsetSet(1,0, 0x3b0, 0x3b0, 0x3b0,0x3b0,1);
			break;
		case 6:
			kHAL_SetWBOffsetSet(1,0, 0x3a0, 0x3a0, 0x3a0,0x3a0,1);
			break;
		case 7:
			kHAL_SetWBOffsetSet(1,0, 0x390, 0x390, 0x390,0x390,1);
			break;
	}

	if (espline >= ftotal ) {
		tmp0 =espline-ftotal;
		espline = ftotal-1;
	}
	else {
		tmp0 =0;
	}*/

	lastt_gain = lastt_gain&0x0fff;

	/**/if(lastt_gain>0x160)
	{
		lastt_gain = lastt_gain>>1;
		espline=espline*2;
	}

	regaddr[0] = 0x03;
	regdata[0] = ((espline>>8)& 0xFF);
	regaddr[1] = 0x04;
	regdata[1] =(espline& 0xFF);
	HAL_WriteSSC(regaddr, regdata, 0x02,  0x02);

	regaddr[0] = 0x0a;
	regdata[0]=((lastt_gain>>8)&0x0f);
	regaddr[1] = 0x0b;
	regdata[1]=(lastt_gain&0xff);
	regaddr[2] = 0x0c;
	regdata[2]=((lastt_gain>>8)&0x0f);
	regaddr[3] = 0x0d;
	regdata[3]=(lastt_gain&0xff);
	regaddr[4] = 0x0e;
	regdata[4]=((lastt_gain>>8)&0x0f);
	regaddr[5] = 0x0f;
	regdata[5]=(lastt_gain&0xff);
	regaddr[6] = 0x10;
	regdata[6]=((lastt_gain>>8)&0x0f);
	regaddr[7] = 0x11;
	regdata[7]=(lastt_gain&0xff);
	HAL_WriteSSC(regaddr, regdata, 0x08,  0x02);
#endif


}

UINT8 HAL_SnapRaw8(UINT8 mode, UINT8 opt) USING_0
{
	mode = mode;

	if( opt == 1 ) {
		HAL_FrontVdWait(1,1);
		HAL_ExpTransToSnap();
		HAL_FrontVdWait(0,1);
	}

	HAL_FrontVdWait(0,1); // 20081024 mantis 32544 wenguo
	HAL_CdspWriteSkip( 6, 1 );
	HAL_FrontVdWait(0,1);
	HAL_FrontVdWait(1,1);
	HAL_FrontInputGate(0x11);
	HAL_CDSPeofWait(1);
	return 0;
}

#ifndef HAL_FRONT_REMOVE_UNCALL_FUNCTION
void HalSensorPrePV(
	void
)
{
	HAL_FrontVdWait(0,1);
	HAL_FrontVdWait(0,1);
	return;
}
#endif

#if 1 /*cs added for gain*/
UINT8 HAL_MaxGainIndexGet()
{
	return(MAX_GAIN_IDX);
}
UINT8 HAL_MinGainIndexGet()
{
	return(MIN_GAIN_IDX);
}

UINT8 HAL_MaxExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MAX_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MAX_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MAX_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MAX_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

UINT8 HAL_MinExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MIN_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MIN_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MIN_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MIN_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

void HAL_ColorSatReduce_Intr(UINT8 idx)	   // reduce color saturation and edge
{
#if 0
	if(idx > K_GAIN_Large) {
		if( sensorDriverGainClass != 3 ) {
			HAL_CdspColorMSet_Intr(cmt[2][0],cmt[2][1],cmt[2][2],cmt[2][3],cmt[2][4],cmt[2][5],cmt[2][6],cmt[2][7],cmt[2][8]);
			XBYTE[0x21D3] = 0x08;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 3;
		}
	}
	else if (idx > K_GAIN_Middle) {
		if( sensorDriverGainClass != 2 ) {
			HAL_CdspColorMSet_Intr(cmt[1][0],cmt[1][1],cmt[1][2],cmt[1][3],cmt[1][4],cmt[1][5],cmt[1][6],cmt[1][7],cmt[1][8]);
			XBYTE[0x21D3] = 0x06;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 2;
		}
	}
	else if (idx > K_GAIN_Small) {
		if( sensorDriverGainClass != 1 ) {
			HAL_CdspColorMSet_Intr(cmt[0][0],cmt[0][1],cmt[0][2],cmt[0][3],cmt[0][4],cmt[0][5],cmt[0][6],cmt[0][7],cmt[0][8]); // for hgnoise
			XBYTE[0x21D3] = 0x04;
			XBYTE[0x21D0] = 0x0A;

			sensorDriverGainClass = 1;
		}
	}
	else {
		if( sensorDriverGainClass != 0 ) {
			cdspColorMatrixRestore_Intr();
			XBYTE[0x21D3] = 0x02;
			XBYTE[0x21D0] = 0x02;

			sensorDriverGainClass = 0;
		}
	};
#endif
}
#endif
#endif


